package com.ikingtech.platform.service.oss.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.base.model.PageResult;
import com.ikingtech.framework.sdk.oss.model.OssFileQueryParamDTO;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.oss.entity.OssFileDO;
import com.ikingtech.platform.service.oss.mapper.OssFileMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author tie yan
 */
@Service
@RequiredArgsConstructor
public class OssService extends ServiceImpl<OssFileMapper, OssFileDO> {

    public OssFileDO getByFileUrl(String fileUrl) {
        return this.getOne(Wrappers.<OssFileDO>lambdaQuery().eq(OssFileDO::getUrl, fileUrl));
    }

    public void removeByFileUrl(String fileUrl) {
        this.remove(Wrappers.<OssFileDO>lambdaQuery().eq(OssFileDO::getUrl, fileUrl));
    }

    public boolean exist(String id) {
        return this.baseMapper.exists(Wrappers.<OssFileDO>lambdaQuery().eq(OssFileDO::getId, id));
    }

    public PageResult<OssFileDO> listPage(OssFileQueryParamDTO queryParam) {
        return PageResult.build(this.page(new Page<>(queryParam.getPage(), queryParam.getRows()), Wrappers.<OssFileDO>lambdaQuery()
                .like(Tools.Str.isNotBlank(queryParam.getName()), OssFileDO::getOriginName, queryParam.getName())
                .in(Tools.Coll.isNotBlank(queryParam.getIds()), OssFileDO::getId, queryParam.getIds())));
    }
}
